﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// tabUI组件实现ToggleGroup效果
/// </summary>
[RequireComponent(typeof(ToggleGroup))]
public class Tab : MonoBehaviour
{

    private ToggleGroup toggleGroup;
    private List<Toggle> toggles;
    private Toggle currentSelectToggle;
    private int currentSelectToggleIndex;
    private List<Action<string, int>> selectedListener;
    private List<Action<string, int>> unselectedListener;
    private bool isInit = false;
    private void Awake()
    {
        Init();
    }
    public void Init()
    {
        if (isInit)
        {
            return;
        }
        isInit = true;
        selectedListener = new List<Action<string, int>>(5);
        unselectedListener = new List<Action<string, int>>(5);
        toggles = new List<Toggle>();
        toggleGroup = GetComponent<ToggleGroup>();
        GetComponentsInChildren(true, toggles);
        for (int i = 0; i < toggles.Count; i++)
        {
            Toggle toggle = toggles[i];
            toggle.group = toggleGroup;
            toggle.onValueChanged.AddListener(OnTabChange);
            if (toggle.isOn)
            {
                if (currentSelectToggle == null)
                {
                    currentSelectToggle = toggle;
                }
                else
                {
                    toggle.isOn = false;
                }
            }
        }
    }
    public string GetSelectedName()
    {
        if (currentSelectToggle != null)
        {
            return currentSelectToggle.name;
        }
        return null;
    }
    public void SetSelectedName(string toggleName)
    {
        for (int i = 0; i < toggles.Count; i++)
        {
            if (toggles[i].name == toggleName)
            {
                if (!toggles[i].isOn)
                {
                    toggles[i].isOn = true;
                }
                break;
            }
        }
    }
    public int GetSelectedIndex()
    {
        if (currentSelectToggle != null)
        {
            return toggles.IndexOf(currentSelectToggle);
        }
        return -1;
    }
    public void SetSelectedIndex(int index)
    {
        if (index >= 0 && index < toggles.Count)
        {
            toggles[index].isOn = true;
        }
    }
    public int AddToggle(Toggle toggle)
    {

        if (toggle != null)
        {
            bool isExist = toggles.Contains(toggle);

            if (!isExist)
            {
                toggle.isOn = false;
                toggle.group = toggleGroup;
                toggles.Add(toggle);
                return toggles.Count;
            }
        }
        return -1;
    }
    public void RemoveToggle(Toggle toggle)
    {
        if (toggle != null)
        {
            int index = toggles.IndexOf(toggle);
            if (index >= 0)
            {
                toggles.RemoveAt(index);
                if (currentSelectToggle == toggle)
                {
                    currentSelectToggle = null;
                    currentSelectToggleIndex = -1;
                    if (toggles.Count > 0)
                    {
                        currentSelectToggle = toggles[0];
                        currentSelectToggle.isOn = true;
                        currentSelectToggleIndex = 0;
                    }
                }
                else
                {
                    for (int i = index; i < toggles.Count; i++)
                    {
                        if (currentSelectToggle == toggles[i])
                        {
                            currentSelectToggleIndex = i;
                            break;
                        }
                    }
                }
            }
        }
    }
    public void AddSelectedListener(Action<string, int> listener)
    {
        Init();
        selectedListener.Add(listener);
    }
    public void AddUnselectedListener(Action<string, int> listener)
    {
        unselectedListener.Add(listener);
    }
    public void RemoveSelectedListener(Action<string, int> listener)
    {
        selectedListener.Remove(listener);
    }
    public void RemoveAllSelectedListeners()
    {
        selectedListener.Clear();
    }
    public void RemoveUnselectedListener(Action<string, int> listener)
    {
        unselectedListener.Remove(listener);
    }
    public void RemoveAllUnselectedListeners()
    {
        unselectedListener.Clear();
    }
    private void OnTabChange(bool isSelect)
    {
        if (isSelect)
        {
            for (int i = 0; i < toggles.Count; i++)
            {
                Toggle toggle = toggles[i];
                if (toggle.isOn)
                {
                    if (toggle == currentSelectToggle)
                    {
                        return;
                    }
                    if (currentSelectToggle != null)
                    {
                        CallUnselectedListener(currentSelectToggle, currentSelectToggleIndex + 1);
                    }
                    currentSelectToggle = toggle;
                    currentSelectToggleIndex = i;
                    CallSelectedListener(currentSelectToggle, currentSelectToggleIndex + 1);
                    break;
                }
            }
        }
    }
    private void CallSelectedListener(Toggle toggle, int index)
    {
        for (int i = selectedListener.Count - 1; i >= 0; i--)
        {
            selectedListener[i]?.Invoke(toggle.name, index);
        }
    }
    private void CallUnselectedListener(Toggle toggle, int index)
    {
        for (int i = unselectedListener.Count - 1; i >= 0; i--)
        {
            unselectedListener[i]?.Invoke(toggle.name, index);
        }
    }
    private void OnDestroy()
    {
        RemoveAllSelectedListeners();
        RemoveAllUnselectedListeners();
        toggles.Clear();
        selectedListener = null;
    }
}
